package com.wiscamp.ninechapters.accounts.application.servies;

import com.wiscamp.ninechapters.accounts.application.assemblers.AccountAssembler;
import com.wiscamp.ninechapters.accounts.application.dto.command.DepositCommand;
import com.wiscamp.ninechapters.accounts.application.dto.command.WithdrawCommand;
import com.wiscamp.ninechapters.accounts.application.dto.vo.AccountLogVO;
import com.wiscamp.ninechapters.accounts.application.dto.vo.AccountVO;
import com.wiscamp.ninechapters.accounts.domain.services.AccountService;
import cube.ddd.aspect.AopAppServiceException;
import com.wiscamp.ninechapters.common.core.CollectionHelper;
import com.wiscamp.ninechapters.common.core.DateTimeHelper;
import com.wiscamp.ninechapters.ddd.ApplicationService;
import jakarta.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Objects;

@Service
public class AccountAppService implements ApplicationService {

    @Autowired
    private AccountService accountService;


    public AccountAppService(AccountService accountService) {
        this.accountService = accountService;
    }

    @AopAppServiceException
    public AccountVO getAccountByType(@Min(0) int accountType, @Min(0) long userId) {
        var account = accountService.getAccountByType(accountType, userId);
        if (Objects.nonNull(account))
            return AccountAssembler.Instance.toAccountVO(account);
        return null;
    }

    @AopAppServiceException
    public Collection<AccountLogVO> getAccountLogs(@Min(0) int accountType, @Min(0) long userId) {
        var logs = accountService.getAccountLogs(accountType, userId);
        return CollectionHelper.convertToArrayList(logs, AccountAssembler.Instance::toAccountLogVO);
    }

    @AopAppServiceException
    public boolean deposit(DepositCommand command) {
        return accountService.deposit(command.getAccountType(), command.getUserId(), command.getOperatorId(), DateTimeHelper.parseDateTime(command.getOperateDate()), command.getRemark(), BigDecimal.valueOf(command.getAmount()));
    }

    @AopAppServiceException
    public boolean withdraw(WithdrawCommand command) {
        return accountService.withdraw(command.getAccountType(), command.getUserId(), command.getOperatorId(), DateTimeHelper.parseDateTime(command.getOperateDate()), command.getRemark(), BigDecimal.valueOf(command.getAmount()));
    }
}
